package ru.stepuk.modern.util;

import edu.uci.ics.jung.algorithms.layout.GraphElementAccessor;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.control.AbstractPopupGraphMousePlugin;
import java.awt.Component;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;
import javax.swing.JPopupMenu;
import ru.stepuk.modern.Path;
import ru.stepuk.modern.gui.PopupMenus.NodeMenu;
import ru.stepuk.modern.gui.PopupMenus.PathMenu;
import ru.stepuk.modern.nodes.Node;
import ru.stepuk.modern.gui.PopupMenus;

/**
 *
 * @author ivan
 */
public class PopupMenuPlugin extends AbstractPopupGraphMousePlugin {

    private PopupMenus.PathMenu pathPopup;
    private PopupMenus.NodeMenu nodePopup;

    public PopupMenuPlugin(PathMenu pathPopup, NodeMenu nodePopup) {
        this(MouseEvent.BUTTON3_MASK);
        this.pathPopup = pathPopup;
        this.nodePopup = nodePopup;
    }

    public PopupMenuPlugin(int modifiers) {
        super(modifiers);
    }

    @Override
    protected void handlePopup(MouseEvent e) {
        final VisualizationViewer<Node, Path> vv =
                (VisualizationViewer<Node, Path>) e.getSource();
        Point2D p = e.getPoint();

        GraphElementAccessor<Node, Path> pickSupport = vv.getPickSupport();
        if (pickSupport != null) {
            final Node node =
                    pickSupport.getVertex(vv.getGraphLayout(), p.getX(), p.getY());
            if (node != null) {
                nodePopup.update(vv, node, null);
                nodePopup.show(vv, e.getX(), e.getY());
            } else {
                final Path path =
                        pickSupport.getEdge(vv.getGraphLayout(), p.getX(), p.getY());
                if (path != null) {
                    pathPopup.update(vv, null, path);
                    pathPopup.show(vv, e.getX(), e.getY());
                }
            }
        }
    }
   
}
